"use strict";

uk.states.play = {
	/** ui配置数据 */
	preload: function(){		
		uk.netbind(uk.netevents.ENTER,this.on_user_enter,this,false);
		uk.netbind(uk.netevents.OTHERENTER,this.on_user_enter,this,false);
		/**放置到最前面 */
		uk.netbind(uk.netevents.LEAVE,this.on_user_leave,this,true);
		uk.netbind(uk.netevents.SETTURN,this.on_set_turn,this,false);
		uk.netbind(uk.netevents.GAMEEND,this.on_game_end,this,false);
		uk.netbind(uk.netevents.PROPCHANGE,function(obj){
			console.log('属性变更 in',obj);
			var r = uk.logic.users[obj.id];
			if(r)/**确保还在房间里才刷新 */
			 	this.refresh_userinfo(r);
			},this,false);
	},
	
	create: function(){
		this.bg = this.game.add.sprite(0,0,'playbg');
		this.bg.width = this.stage.width;
		this.bg.height = this.stage.height;

		/**建几个层来放一些东西 */
		this.uilayer = this.game.add.group();
		this.cardlayer = this.game.add.group();
		this.tiplayer = this.game.add.group();

		var upd = uk.uidata.play;
		this.userinfo={};
		for(var i in uk.logic.users){
			this.__create_user_infoui(uk.logic.get_local_seat(uk.logic.users[i]));
		}

		this.dealcard0 = this.game.add.image(upd.POOLCARDPOS[0].x,upd.POOLCARDPOS[0].y,'cardback');
		this.dealcard1 = this.game.add.image(upd.POOLCARDPOS[1].x,upd.POOLCARDPOS[1].y,'cardback');
		this.dealcard0_num = this.game.add.text(this.dealcard0.width/2,this.dealcard0.height/2,'x',uk.uidata.fontstyle.remaincardnum);
		this.dealcard1_num = this.game.add.text(this.dealcard1.width/2,this.dealcard1.height/2,'x',uk.uidata.fontstyle.remaincardnum);
		this.dealcard0_num.setScaleMinMax(1,1,1,1);
		this.dealcard1_num.setScaleMinMax(1,1,1,1);
		this.dealcard0.addChild(this.dealcard0_num);
		this.dealcard1.addChild(this.dealcard1_num);
		this.dealcard0_num.anchor.set(0.5);
		this.dealcard1_num.anchor.set(0.5);
		this.dealcard0.width = upd.POOLCARDSIZE.cx;
		this.dealcard0.height = upd.POOLCARDSIZE.cy;
		this.dealcard1.width = upd.POOLCARDSIZE.cx;
		this.dealcard1.height = upd.POOLCARDSIZE.cy;
		
		this.deadcard0 = this.game.add.image(upd.DEADCARDPOS[0].x,upd.DEADCARDPOS[0].y,'carddead');
		this.deadcard1 = this.game.add.image(upd.DEADCARDPOS[1].x,upd.DEADCARDPOS[1].y,'carddead');
		this.deadcard0.width = upd.DEADCARDSIZE[0].cx;
		this.deadcard0.height = upd.DEADCARDSIZE[0].cy;
		this.deadcard1.width = upd.DEADCARDSIZE[1].cx;
		this.deadcard1.height = upd.DEADCARDSIZE[1].cy;

		this.uilayer.add(this.deadcard0);
		this.uilayer.add(this.deadcard1);

		this.btn_finishround = uk.game.add.button(upd.FINISHROUNDPOS.x,upd.FINISHROUNDPOS.y,'play.btnfinishround',function(){
			uk.netsend(uk.netevents.FINISHROUND,{});
		},this,1,0);
        this.btn_finishroundtxt = uk.game.add.text(20,this.btn_finishround.height/2+2,'',uk.uidata.fontstyle.clocknum);
		this.btn_finishround.addChild(this.btn_finishroundtxt);
		this.btn_finishroundtxt.anchor.set(0.5);
		this.btn_finishround.ignoreChildInput = true;
		this.uilayer.add(this.btn_finishround);

		/**时钟 */
		this.timer = this.game.time.create(true);

		console.log('play create');	
		uk.netcache_enable(false);

		/**刷新下玩家的信息 */
		for(var u in uk.logic.seats)
			this.refresh_userinfo(uk.logic.seats[u]);
	},

	shutdown:function(){
		console.log('unbind play');
		/**解绑和我有关的所有消息 */
		uk.netunbind(null,this);
	},

	on_user_enter:function(obj){
		var r = obj.id?uk.logic.users[obj.id]:uk.logic.self;
		var ls = uk.logic.get_local_seat(r);
		if(!this.userinfo[ls])
			this.__create_user_infoui(ls);
		this.refresh_userinfo(r);
	},

	on_user_leave:function(obj){
		this.refresh_userinfo(uk.logic.users[obj.id]);
	},

	__create_user_infoui:function(localseat){
		console.log('create userinfo ui',localseat);
		var upd = uk.uidata.play;
		var u = {};
		var i = localseat;
		u.bg = this.game.add.image(0,0,'play.userinfobg'+i);
		this.uilayer.add(u.bg);
		if(i == 0){
			u.bg.y = uk.viewsize.cy - u.bg.height - 2;
		}
		u.nametxt = this.game.add.text(upd.USERINFONAMEPOS[i].x,upd.USERINFONAMEPOS[i].y,'',uk.uidata.fontstyle.userinfoname);
		u.hp = this.game.add.graphics(upd.USERINFOHPPOS[i].x,upd.USERINFOHPPOS[i].y);
		u.mp = this.game.add.graphics(upd.USERINFOMPPOS[i].x,upd.USERINFOMPPOS[i].y);
		u.exp = this.game.add.graphics(upd.USERINFOEXPPOS[i].x,upd.USERINFOEXPPOS[i].y);
		uk.gui.fill_graphics_rect(u.hp,0xFF9999,upd.USERINFOHPMPSIZE.cx,upd.USERINFOHPMPSIZE.cy);
		uk.gui.fill_graphics_rect(u.mp,0x99CCFF,upd.USERINFOHPMPSIZE.cx,upd.USERINFOHPMPSIZE.cy);		
		uk.gui.fill_graphics_rect(u.exp,0xCCFF99,upd.USERINFOEXPSIZE.cx,upd.USERINFOEXPSIZE.cy);

		u.hptxt = this.game.add.text(upd.USERINFOHPPOS[i].x+upd.USERINFOHPMPSIZE.cx/2,upd.USERINFOHPPOS[i].y+2+upd.USERINFOHPMPSIZE.cy/2,'',uk.uidata.fontstyle.userinfoprogress);
		u.mptxt = this.game.add.text(upd.USERINFOMPPOS[i].x+upd.USERINFOHPMPSIZE.cx/2,upd.USERINFOMPPOS[i].y+2+upd.USERINFOHPMPSIZE.cy/2,'',uk.uidata.fontstyle.userinfoprogress);
		u.exptxt = this.game.add.text(upd.USERINFOEXPPOS[i].x+upd.USERINFOEXPSIZE.cx/2,upd.USERINFOEXPPOS[i].y+2+upd.USERINFOEXPSIZE.cy/2,'',uk.uidata.fontstyle.userinfoprogress);
		
		u.nametxt.inputEnabled = false;
		u.hp.inputEnabled = false;
		u.mp.inputEnabled = false;
		u.bg.addChild(u.nametxt);
		u.bg.addChild(u.hp);
		u.bg.addChild(u.mp);
		u.bg.addChild(u.exp);
		u.bg.addChild(u.hptxt);
		u.bg.addChild(u.mptxt);
		u.bg.addChild(u.exptxt);

		u.nametxt.anchor.set(0.5,0.5);
		u.hptxt.anchor.set(0.5,0.5);
		u.mptxt.anchor.set(0.5,0.5);
		u.exptxt.anchor.set(0.5,0.5);

		this.userinfo[i]=u;
	},

	refresh_userinfo:function(r){
		console.log('refresh_userinfo ui',r);
		var upd = uk.uidata.play;
		var ls = uk.logic.get_local_seat(r);
		var props = r.props;
		this.userinfo[ls].nametxt.text = r.name;
		this.userinfo[ls].hptxt.text = props.hp+'/'+props.maxhp;
		this.userinfo[ls].mptxt.text = props.mp+'/'+props.maxmp;
		this.userinfo[ls].exptxt.text = props.exp+'/'+props.maxexp;
		this.userinfo[ls].hp.width=upd.USERINFOHPMPSIZE.cx * (props.hp/props.maxhp);
		this.userinfo[ls].mp.width=upd.USERINFOHPMPSIZE.cx * (props.mp/props.maxmp);		
		this.userinfo[ls].exp.width=upd.USERINFOEXPSIZE.cx * (props.exp/props.maxexp);
	},

	on_set_turn:function(obj){
		this.timer.removeAll();
		if(uk.logic.self.seat == obj.turn){
			this.btn_finishround.visible = true;
			this.roundtime = obj.timeout;
			this.btn_finishroundtxt.text=this.roundtime;
			this.timer.loop(1000,function(){
				 this.btn_finishroundtxt.text=--this.roundtime;
			},this);
			this.timer.start();
		}else{
			this.btn_finishround.visible = false;
		}
	},

	on_game_end:function(obj){
		this.timer.removeAll();
		this.btn_finishround.visible = false;
	},

	update: function(){
	},

	render:function(){/*
		var role = uk.logic.self;
		this.game.debug.text(role.name,5,405,'rgb(0,255,0)');
		this.game.debug.text(role.template.name,5,435,'rgb(0,255,0)');
		var y = 455;
		for(var i in role.props){
			this.game.debug.text(i+':'+role.props[i],5,y,'rgb(0,255,0)');
			y+=20;
		}

		role = null;
		for(var i in uk.logic.seats){
			var r = uk.logic.seats[i];
			if(r == null) continue;
			if(r.id != uk.logic.self.id){
				role = r;
				break;
			}
		}
		if(role == null)
			return;
		this.game.debug.text(role.name,5,15,'rgb(0,255,0)');
		this.game.debug.text(role.template.name,5,35,'rgb(0,255,0)');
		var y = 55;
		for(var i in role.props){
			this.game.debug.text(i+':'+role.props[i],5,y,'rgb(0,255,0)');
			y+=20;
		}
		*/
	},

	set_dealcard_remain:function(localseat,num){
		if(localseat == 0)
			this.dealcard0_num.text = num;
		else
			this.dealcard1_num.text = num;
	}
};